<?php
namespace app\index\controller;
use think\Controller;
use app\admin\model\System as SystemModel;
use app\admin\model\Course as CourseModel;
use app\admin\model\Student as StudentModel;
use app\admin\model\Enrollment as EnrollmentModel;

class Enrollment extends BaseController
{
    public function index()
    {
        //取出学生信息
        //$student = StudentModel::get(session('id'));
        $student = StudentModel::with('enrollments')->find(session('id'));
        $school_id = $student->school_id;
        $grade = $student->grade;
        $gender = $student->gender;
        
        //判断是否可以选课
        $system = SystemModel::get(1);
        if($system->system_on==0){
            $this->redirect('info');
        }

        //如果学生已经选了课，就跳到结果页面。
        if(count($student->enrollments)>0){
            return redirect('resultpage');
        }
        
        //post提交
        if(request()->isPost())
        {
            $enrollment = new EnrollmentModel;
            $course_id = input('post.course_id');
            $stu_id = $student->stu_id;

            //如果课程数已满，则跳到选课页面有提示。
            $course = CourseModel::with('enrollments')->find($course_id);
            //dump(count($course->enrollments));die;
            if(count($course->enrollments) >= $course->course_number){
                $this->redirect('fullinfo');
            }

            $data = [
                'course_id'=> $course_id,
                'stu_id'=> $stu_id
            ];
            //dump($data);die;
            $result = $enrollment->validate(
                [
                    'course_id' => 'require',
                    'stu_id' => 'require',
                    '__token__' => 'token',
                ]
            )->save($data);
            if (false === $result) {
                $this->error($enrollment->getError());                
            } else {
                return redirect('resultpage');
            }
        }

        // $courses = new CourseModel();
        // $list = $courses->where('school_id',$school_id)
        //                 ->where('grade', $grade)
        //                 ->where('gender','in','2,$gender')
        //                 ->select();

        $list = CourseModel::with('enrollments')
                        ->where('school_id',$school_id)
                        ->where('grade', $grade)
                        ->where('gender','in',"2,$gender")
                        ->select();

        //dump($list);die;
        $this->assign('list',$list);      
        return $this->fetch();
    }

    public function resultpage()
    {
        $student = StudentModel::get(session('id'));
        //dump($student);die;
        $enrollments = EnrollmentModel::where('stu_id','=',session('id'))->select();
        //dump($enrollments);die;
        $this->assign('student',$student);
        $this->assign('list',$enrollments);
        return $this->fetch();
    }

    public function info()
    {
        //取出学生信息
        $student = StudentModel::get(session('id'));
        $this->assign('student',$student);
        return $this->fetch();
    }

    public function fullinfo()
    {
        //取出学生信息
        $student = StudentModel::get(session('id'));
        $this->assign('student',$student);
        return $this->fetch();
    }

    
    public function test()
    {
        // $enrollment = new EnrollmentModel;
        // $enrollment->course_id = 6;
        // $enrollment->stu_id = 4;
        // $enrollment->save();

        // echo 'ok';

        // $enrollment = EnrollmentModel::get(2);
        // echo $enrollment->enrollment_date;

        $course = CourseModel::get(7);
        echo $course->course_name;
        echo "<br/>";
        echo count($course->enrollments);
    }
}